#include <cstdlib>
#include <iostream>
using namespace std;

#include "outputstream.hh"

OutputStream::OutputStream()
{
  _buf = (char*)malloc(4096);
  _buf[4095] = '\0';
  _index = _buf;
  _capacity = 4096;
}

void OutputStream::adjust(long allocSize)
{
  long size = _index - _buf;
  while(size + allocSize >= _capacity){
    _capacity = _capacity * 2;
    _buf = (char*)realloc(_buf,_capacity);
    _index = _buf + size;    
  }
}

void OutputStream::append(char* pLine, long sizeOfLine)
{
  adjust(sizeOfLine);
  memcpy(_index,pLine,sizeOfLine);
  _index += sizeOfLine;
  *_index = '\0';
}

void OutputStream::print()
{
  cout << _buf << endl;
}

